import { IBaseBehavior } from "../../base/IBaseBehavior";
import { BT_Type } from "../../BTDefine";
import { BTFactory } from "../../BTFactory";
import { BaseBehavior } from "../BaseBehavior";

export abstract class Composite extends BaseBehavior {

    protected _children: IBaseBehavior[] = [];

    constructor() {
        super();

        this._type = BT_Type.TYPE_COMPOSITE;
    }

    public setChildren(childList: IBaseBehavior[] | string[]) {
        for (let i = 0; i < childList.length; i++) {
            const child = childList[i];
            this._children.push(BTFactory.getNode(child));
        }
    }

    public getChildren(): IBaseBehavior[] {
        return this._children;
    }

    public addChild(child: IBaseBehavior | string) {
        this._children.push(BTFactory.getNode(child));
    }

    public tick(dt: number): number {
        for (let i = 0; i < this._children.length; i++) {
            const child = this._children[i];
            child.setContext(this._context);
        }

        return super.tick(dt);
    }

    public abort(): number {
        for (let i = 0; i < this._children.length; i++) {
            const child = this._children[i];
            child.abort();
        }

        return super.abort();
    }

    public reset(): void {
        super.reset();
        // this._children.length = 0;
    }
}